/*
 * AbstractDrawPage.h
 *
 *  Created on: 2010-9-26
 *      Author: wenjian
 */

#ifndef ABSTRACTDRAWPAGE_H_
#define ABSTRACTDRAWPAGE_H_

#include <event/EventListener.h>
#include "../commons.h"
#include "ddatatypes.h"
#include "ModifiedSupport.h"
#include "ModifiedEvent.h"
#include "EventSupport.h"
#include "AbstractLayer.h"


namespace Ed {

  class AbstractDrawPage :
    public ModifiedSupport,
    public Thk::EventListener,
    public EventSupport{

    friend class AbstractLayer;

    public:
      AbstractDrawPage();
      virtual ~AbstractDrawPage();


      /**
       * 返回x坐标
       */
      //virtual real x() const=0;
      virtual real x() const{
        return 0;
      }
      /**
       * 返回y坐标
       */
      //virtual real y() const=0;
      virtual real y() const{
        return 0;
      }

      /**
       * 返回宽度
       */
      //virtual real width() const=0;
      virtual real width() const{
        return 0;
      }

      /**
       * 返回高度
       */
      //virtual real height() const;
      virtual real height() const{
        return 0;
      }

      /**
       * 加入图层
       */
      virtual inline bool addLayer(AbstractLayer* l){
        return addLayer(l, layers.size());
      }

      /**
       * 加入图层, 也可用于调整图层位置, 如果图层已经存在，可改变其位置
       */
      virtual bool addLayer(AbstractLayer* l, int pos);

      /**
       * 移除图层
       */
      virtual bool removeLayer(AbstractLayer* l);


      /**
       * 返回所有层
       */
      virtual inline const std::vector<AbstractLayer*>* getLayers() const {
        return &layers;
      }

      /**
       * 设置鼠标事件响应模式
       */
      virtual inline void setMouseEventMode(MouseEventMode mode, AbstractLayer* layer=NULL){
        mouseEventMode = mode;
        //mouseHoveringLayer = layer;
        manualModeLayer = layer;
      }

      /**
       * 返回鼠标事件响应模式
       */
      virtual inline MouseEventMode getMouseEventMode() const{
        return mouseEventMode;
      }

      /**
       * 返回鼠标事件响应层
       */
      virtual inline AbstractLayer* getMouseHoveringLayer() const{
        return mouseHoveringLayer;
      }

      /**
       * 清除鼠标悬停层
       */
      virtual inline void clearMouseHoveringLayer(){
        mouseHoveringLayer = NULL;
      }

      /**
       * 返回当前焦点层
       */
      virtual inline AbstractLayer* getFocusLayer(){
        return focusLayer;
      }

      /**
       * 设置焦点层
       */
      virtual inline void setFocusLayer(AbstractLayer* l){
        focusLayer = l;
      }

      /**
       * 处理层ModifiedEvent事件, 向画面添加层时，
       * 会自动向层添加ModifiedEvent事件监听器
       */
      virtual bool handle(const Thk::Event* e);

      /**
       * 跟踪响应事件, 向画布，层，或元素传递鼠标、键盘或其它事件
       */
      virtual bool trackEvent(const Thk::Event* e);

    private:
      /**
       * 处理鼠标事件
       */
      bool handleMouseEvent(const MouseEvent* me);

      /**
       * 跟踪层鼠标事件
       */
      bool layerTrackEvent(AbstractLayer* layer, const MouseEvent* me);

      /**
       * 返回当前活动层（鼠标所在位置的层）
       */
      AbstractLayer* getHoveringLayer(real x, real y);

//      /**
//       * 鼠标离开层
//       */
//      void mouseLeaveLayer(AbstractLayer* layer);

    protected:
      //定义图层容器类型
      typedef std::vector<AbstractLayer*> Layers;
      //所有图层, 第位置0为最低层，第layers.size()-1为最顶层
      Layers layers;
      //鼠标事件响应模式
      MouseEventMode mouseEventMode;
      //鼠标悬停层
      AbstractLayer* mouseHoveringLayer;
      //MEventMode_Manual模式下指定层
      AbstractLayer* manualModeLayer;
      //当前焦点层
      AbstractLayer* focusLayer;

  };

}

#endif /* ABSTRACTDRAWPAGE_H_ */
